package com.papa91.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.Collator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;

public class FileUtil
{

	//public static HashMap<String, String> cnNames = null;
	static int mode = 0;
	static Collator chinaCollator;
	static Context context;
	static
	{
		chinaCollator = Collator.getInstance(java.util.Locale.CHINA);
	}

	public static void setMode(Context c, int m, HashMap<String, String> names)
	{
		mode = m;
		//cnNames = names;
	}

	public static FileWrapper[] listSortedFiles(File dirFile)
	{
		//		if (mode == 1 && cnNames != null) {
		//			return listSortedCnFiles(dirFile);
		//		} else
		return listSortedEnFiles(dirFile);
	}

	// list sorted files
	public static FileWrapper[] listSortedEnFiles(File dirFile)
	{
		assert dirFile.isDirectory();

		File[] files = dirFile.listFiles();

		if (files == null)
			return null;

		FileWrapper[] fileWrappers = new FileWrapper[files.length];
		for (int i = 0; i < files.length; i++)
		{
			fileWrappers[i] = new FileWrapper(files[i]);
		}

		Arrays.sort(fileWrappers);
		return fileWrappers;
	}

	public static void Unzip(Resources res, String zipFile, String targetDir)
	{
		int BUFFER = 4096;
		String strEntry;

		try
		{
			BufferedOutputStream dest = null;
			AssetManager am = res.getAssets();
			InputStream fis = am.open(zipFile);
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
			ZipEntry entry; //

			byte data[] = new byte[BUFFER];
			while ((entry = zis.getNextEntry()) != null)
			{
				try
				{
					// Log.i("Unzip: ","="+ entry);
					int count;
					strEntry = entry.getName();

					File entryFile = new File(targetDir + strEntry);
					File entryDir = new File(entryFile.getParent());
					if (!entryDir.exists())
					{
						entryDir.mkdirs();
					}

					FileOutputStream fos = new FileOutputStream(entryFile);
					dest = new BufferedOutputStream(fos, BUFFER);
					while ((count = zis.read(data, 0, BUFFER)) != -1)
					{
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
				} catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
			zis.close();
		} catch (Exception cwj)
		{
			cwj.printStackTrace();
		}
	}

	public static byte[] getIndexFile(String fileName, int index)
	{
		int count = index;
		int size = 0;

		InputStream is = null;

		try
		{
			is = new FileInputStream(new File(fileName));
			//is = (InputStream) m_res.openRawResource(R.raw.text);

			byte b[] = null;

			for (int i = 0; i < count; i++)
			{
				b = null;
				b = new byte[4];
				is.read(b);

				int s0 = b[0];
				int s1 = b[1];
				int s2 = b[2];
				int s3 = b[3];

				s0 = (int) (s0 & 0xFF);
				s1 = (int) ((s1 & 0xFF) << 8);
				s2 = (int) ((s2 & 0xFF) << 16);
				s3 = (int) ((s3 & 0xFF) << 24);

				size = (s0 | s1 | s2 | s3);
				is.skip(size);
			}

			b = null;
			b = new byte[4];
			is.read(b);

			int s0 = b[0];
			int s1 = b[1];
			int s2 = b[2];
			int s3 = b[3];

			s0 = (int) (s0 & 0xFF);
			s1 = (int) ((s1 & 0xFF) << 8);
			s2 = (int) ((s2 & 0xFF) << 16);
			s3 = (int) ((s3 & 0xFF) << 24);

			size = (s0 | s1 | s2 | s3);

			byte data[] = new byte[size];
			is.read(data, 0, size);
			is.close();

			return data;
		} catch (Exception e)
		{
			if (is != null)
				try
				{
					is.close();
				} catch (Exception e1)
				{
					is = null;
				}
		}

		return null;
	}

}
